package knh;

import java.util.*;

import knh.CheckList_Data;
import knh.DB_Items.*;
import android.app.*;
import android.content.*;
import android.database.*;
import android.database.sqlite.*;
import android.os.*;
import android.widget.*;
import com.example.crystall_ball.*;

public class DB_Groups {
	boolean load = false;
	private DBHelper mHelper;
	private int group_limit = 100;
	private String DB_Name = new String("groups");
	
	public int getGroup_Limit(){
		return group_limit;
	}
	
	public DB_Groups(Object obj){
		mHelper = new DBHelper((Context)obj);
		SQLiteDatabase db = mHelper.getWritableDatabase();
		mHelper.onUpgrade(db, 0, 0);
	}
	
	private class DBHelper extends SQLiteOpenHelper{
		public DBHelper(Context context){
			super(context,"group.db",null,1);
		}
		public void onCreate(SQLiteDatabase db){
			db.execSQL("CREATE TABLE "+DB_Name+" ( groupNum INTEGER PRIMARY KEY autoincrement," +
					"name VARCHAR,image INT,tagID TEXT);");
		}
		public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
			db.execSQL("DROP TABLE IF EXISTS "+DB_Name);
			onCreate(db);
		}
	}
	
	public CheckList_Data LoadData(String find_name){
		String tags;
		int image,gnum;
		SQLiteDatabase db = mHelper.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT * FROM "+ DB_Name+" WHERE name = '"+find_name+"'",null);
		
		if(cursor.moveToNext()){
			gnum = Integer.parseInt(cursor.getString(0));
			image = Integer.parseInt(cursor.getString(2));
			tags = cursor.getString(3);
			db.close();
		}else{
			db.close();
			return new CheckList_Data(0,"",-1,"");
		}
		return new CheckList_Data(image,find_name,gnum,tags);
	}
	
	public CheckList_Data[] LoadAllData(){
		CheckList_Data group[] = new CheckList_Data[group_limit];
		String name,tags;
		int image,gnum,cnt;
		SQLiteDatabase db = mHelper.getReadableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("SELECT * FROM "+DB_Name,null);
		cnt = 0;
		while(cursor.moveToNext()){
			gnum = Integer.parseInt(cursor.getString(0));
			name = cursor.getString(1);
			image = Integer.parseInt(cursor.getString(2));
			tags = cursor.getString(3);
			group[cnt++] = new CheckList_Data(image,name,gnum,tags);
		}
		return group;
	}
	
	public boolean SaveData(CheckList_Data group){
		SQLiteDatabase db = mHelper.getWritableDatabase();
		boolean chk = true;
		
		try{
			db.execSQL("INSERT INTO "+ DB_Name+" (name,image,tagID)" +
					"VALUES('"+group.getName()+"',"+group.getImage()+",'"+group.getTagIDbyString()+"');");
		}catch(SQLiteConstraintException e){
			return false;
		}finally{
			db.close();
		}
		return true;
	}
	
	public void DeleteData(int gnum){
		SQLiteDatabase db = mHelper.getWritableDatabase();
		db.execSQL("DELETE FROM " + DB_Name + " WHERE groupNum = " + gnum + ";");
		db.close();
	}
	
	public void DeleteAllData(){
		SQLiteDatabase db = mHelper.getWritableDatabase();
		db.execSQL("DELETE * FROM "+DB_Name+";");
		db.close();
	}
	
	public void UpdateData(CheckList_Data group){
		DeleteData(group.getImage());
		SaveData(group);
	}
}